<?php
namespace app\nadmin\controller;
use think\Validate;
use app\nadmin\model\Adminer_M;

/**
 * @explain 个人设置相关操作类
 * @time 2019/5/5
 * @writer liuhao
 */

class Personage extends Base
{
    //基本信息页面
    public function basedata()
    {
        $this->assign("adminInfo",$this->adminInfo);
        return $this->fetch();
    }

    //安全设置页面
    public function safety()
    {
        return $this->fetch();
    }

    //修改基本信息
    public function updateBaseDate()
    {
        $id = $this->adminerId; //当前管理员ID
        $username = $this->request->username ? $this->request->username : '';
        $portrait = $this->request->portrait ? $this->request->portrait : '';
        $name = $this->request->name ? $this->request->name : '';
        $phone = $this->request->phone ? $this->request->phone : 0;
        $email = $this->request->email ? $this->request->email : '';
        $status = $this->request->status ? $this->request->status : 1;
        $role = $this->request->role;
        $rule = [
            'id' => 'require|number|gt:0',
            'username' => 'require|alphaDash|min:6|max:12',
            'portrait' => ['regex' => '/^\/upload(\/\w+)*.(png|gif|jpg|jpeg)$/'],
            'name' => 'chsDash|min:2|max:12',
            'phone' => 'mobile',
            'email' =>'email',
            'status' => 'require|in:0,1',
        ];

        $msg = [
            'id.require'=>'参数异常 EOO1',
            'id.number'=>'参数异常 EOO2',
            'id.gt'=>'参数异常 EOO3',
            'username.require'=>'账号不能为空',
            'username.alphaDash'=>'账号只能为字母和数字集合',
            'username.min'=>'账号长度不能小于6位',
            'username.max'=>'账号长度不能大于12位',
            'portrait.regex'=>'图片格式异常',
            'name.chsDash'=>'姓名格式错误',
            'name.min'=>'姓名长度能小于2位',
            'name.max'=>'姓名长度能大于12位',
            'phone.mobile'=>'手机格式错误',
            'email.email'=>'邮箱格式错误',
            'status.username'=>'参数异常 EOO4',
        ];
        $data = compact('id','username','portrait','name','phone','email','status');
        $validate = Validate::make($rule,$msg);
        if(!$validate->check($data)){
            return echojson($validate->getError(),0);
        }

        unset($data['id']);
        $where = "id=".$id;

        $adminer_M = new adminer_M();
        $re = $adminer_M->editOne($data,$where);
        if($re){
            //修改管理员缓存数据
            $data['id'] = $id;
            $data['role'] = $role;
            setAdminInfo($data);
            return echojson("修改成功",1);
        }else{
            return echojson("修改失败",0);
        }
    }

    //修改密码
    public function updatePwd()
    {
        $id = $this->adminerId; //当前管理员ID
        $pwd = $this->request->pwd ? $this->request->pwd : '';

        $rule = [
            'id' => 'require|number|gt:0',
            'pwd' =>'require|alphaDash|min:6|max:12', //字母数字下划线
        ];
        $msg = [
            'id.require'=>'参数异常 EOO1',
            'id.number'=>'参数异常 EOO2',
            'id.gt'=>'参数异常 EOO3',
            'pwd.require'=>'密码不能为空',
            'pwd.alphaDash'=>'密码只能为字母和数字集合',
            'pwd.min'=>'密码长度不能小于6位',
            'pwd.max'=>'密码长度不能大于12位',
        ];
        $data = compact('id','pwd');
        $validate = Validate::make($rule,$msg);
        if(!$validate->check($data))
        {
            return echojson($validate->getError(),0);
        };

        unset($data['id']);

        $salt = getRandStr($num=6,$str='');
        //密码
        $data['pwd'] = createPwd($pwd,$salt);
        $data['salt'] = $salt;
        $where = "id=".$id;

        $adminer_M = new adminer_M();
        $re = $adminer_M->editOne($data,$where);
        if($re){
            return echojson("修改成功",1);
        }else{
            return echojson("修改失败",0);
        }
    }

}